package yf.common;

import io.restassured.RestAssured;
import org.testng.ITestNGMethod;
import org.testng.Reporter;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.internal.ConstructorOrMethod;
import yf.annotations.DataSource;
import yf.bean.TestCase;
import yf.dao.DBDPConnection;
import yf.dao.ExcutSqlFile;
import yf.data.DataProviders;
import yf.utils.GetTestCase;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

public class setUpTearDown {
//    @BeforeSuite
    public void dataSetUp() throws SQLException, IOException, ClassNotFoundException {
        ExcutSqlFile.excute(new getUpSqlName().invok().upSqlName ); //案例执行前参数维护
        System.out.println("案例执行前参数维护");
    }

    //环境配置
    @BeforeClass
    public  void envSetUp() {
        try {
            InputStream in = DBDPConnection.class.getResourceAsStream("/env.properties");
            Properties properties = new Properties();
            properties.load(in);
            RestAssured.baseURI = properties.getProperty("url");
            RestAssured.port = Integer.parseInt(properties.getProperty("port"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    @AfterSuite
    public void dataTearDown() throws SQLException, IOException, ClassNotFoundException {
        //案例执行结束后，对数据池的数据进行清理（删除或更新状态）
        ExcutSqlFile.excute(new getUpSqlName().invok().downSqlName); //案例执行后数据清理
    }
    private class getUpSqlName{
        private String upSqlName;
        private String downSqlName;
        ITestNGMethod iTestNGMethod = Reporter.getCurrentTestResult().getMethod();
        ConstructorOrMethod constructorOrMethod = iTestNGMethod.getConstructorOrMethod();
        DataSource dataSource = constructorOrMethod.getMethod().getAnnotation(DataSource.class);
        /**
         * 调用
         * */
        public getUpSqlName invok() throws IOException {

            upSqlName=dataSource.upSqlName();
            downSqlName=dataSource.downSqlName();
            return this;
        }
        /**
         * 获取LocatorSource注解中的locator filePath
         * */
        public String upSqlName() {

            return upSqlName;
        }
        public String downSqlName(){
            return downSqlName;
        }

    }

}
